﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class chapter : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Bind();
        }
    }

    public void Bind()
    {
        DBOperate mDB = new DBOperate();
        mDB.SetCommandString("SELECT * FROM ChapterList");
        chapterlist.DataSource = mDB.ExecuteReader();
        chapterlist.DataBind();
        mDB.CloseDB();
    }

    protected void chapterlist_RowEditing(object sender, GridViewEditEventArgs e)
    {
        //Bind();
        chapterlist.EditIndex = e.NewEditIndex;
        Bind();
    }

    protected void chapterlist_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        chapterlist.EditIndex = -1;
        Bind();
    }

    protected void chapterlist_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string sqlstr = "DELETE FROM [ChapterList] WHERE [No] = "+ chapterlist.DataKeys[e.RowIndex].Value.ToString() + "";
        //WHERE [No] = 后面不能加''，否则会报错。
        //异常详细信息: System.Data.OleDb.OleDbException: 标准表达式中数据类型不匹配。
        DBOperate mDB = new DBOperate();
        mDB.SetCommandString(sqlstr);
        mDB.ExecuteNonReader();
        Bind();
    }

    protected void chapterlist_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string sqlstr = "UPDATE [ChapterList] SET [CNo] = " 
                        + ((TextBox)(chapterlist.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim()
                        + ", [Cname] = '" + ((TextBox)(chapterlist.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim() 
                        + "' WHERE [No] = " + chapterlist.DataKeys[e.RowIndex].Value.ToString() + "";
        DBOperate mDB = new DBOperate();
        mDB.SetCommandString(sqlstr);
        mDB.ExecuteNonReader();
        chapterlist.EditIndex = -1;
        Bind();
    }


    
}
